#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   acc.py    
@Contact :   raogx.vip@hotmail.com
@License :   (C)Copyright 2017-2018, Liugroup-NLPR-CASIA

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2020/10/7 3:16 下午   gxrao      1.0         None
'''

# import lib
def acc(prediction, really):
    """
    计算视频检测算法的准确性
    :param prediction: 预测时间段
    :param really: 真实时间段
    :return: 准确率
    """
    union = 0
    really_total = 0
    prediction_total = 0
    # Compute union
    for pred in prediction:
        for real in really:
            if real[0] > pred[1] or real[1] < pred[0]:
                continue
            else:
                union += min(real[1], pred[1]) - max(real[0], pred[0])

    for pred in prediction:
        prediction_total += pred[1] - pred[0]

    for real in really:
        really_total += real[1] - real[0]
    if prediction_total + really_total - union == 0:
        return 0.0
    return union / (prediction_total + really_total - union) * 1.0